package com.alan.shiro.api.controller;

import com.alan.shiro.api.bean.ResultMap;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Auther: Alan liu
 * @Date: 2018/12/11 19:16
 * @Description: ExceptionController
 */
@RestControllerAdvice
public class ExceptionController {

    /**
     *  捕捉 CustomRealm 抛出的异常
     * @param ex
     * @return
     */
    @ExceptionHandler(AccountException.class)
    public ResultMap handleShiroException(Exception ex) {
        System.out.println("handleShiroException :" +ex.getMessage());
        return new ResultMap().fail().message(ex.getMessage());
    }

    /**
     * 捕捉 AuthorizationException 抛出的异常
     * @param ex
     * @return
     */
    @ExceptionHandler(AuthorizationException.class)
    public ResultMap handleAuthorizationException(Exception ex) {
        System.out.println("handleShiroException :" +ex.getMessage());
        return new ResultMap().fail().message(ex.getMessage());
    }

}
